<template>
  <div>
    <Modal
      v-model="modals"
      width="1000"
      scrollable
      closable
      :title="titleFrom"
      :mask-closable="false"
      :z-index="1"
      @on-cancel="handleReset"
	  @on-confirm="handleSubmit"
      @on-visible-change="visible"
    >
      <Form ref="formValidate" :model="formValidate" :label-width="110" :rules="ruleValidate" @submit.native.prevent>
        <Row type="flex" :gutter="24">
          	<div class="input_top">
          		  <div class="left">
          			<!-- <Input v-model="formValidate.no" placeholder="姓名/手机号码/病历号"></Input> -->
					<Select filterable="true" v-model="formValidate.uid" placeholder="请选择" element-id="uid" clearable @click.native="handleChange">
						<Option v-for="(item, j) in users" :value="item.id" :key="j">{{ item.name }}</Option>
					</Select>
          			<Button type="primary" icon="md-add" class="button" @click="addArchive()">建档</Button>
          		  </div>
          		  <div class="right">
          			 <span class="label">日期：</span>
          			 <DatePicker
          			   class="date"
          			   type="date"
          			   :value="sche_date"
          			   placeholder="请选择日期"
          			   style="width: 100%"
          			   format="yyyy-MM-dd"
          			   @on-change="selectDate"
          			 ></DatePicker>
          		  </div>
          	</div>
        </Row><!--row end-->
		<Row type="flex" :gutter="24">
		  	<Col v-bind="grid">
		        <div class="user_info">
					<img class="avatar" src="../../../../assets/images/f.png"></img>
					<div class="info_defail">
						<div class="name">{{showUserInfo.name}}/{{showUserInfo.sex}}/{{showUserInfo.age}}</div>
						<div class="property_info">
							<div class="section">
								<div class="column">
									<div class="label">病历号：</div><div class="text">{{showUserInfo.no}}</div>
								</div>
								<div class="column">
									<div class="label">建档日期：</div><div class="text">{{showUserInfo.created_time}}</div>
								</div>
							</div>
							<div class="section">
								<div class="column">
									<div class="label">手机号码：</div><div class="text">{{showUserInfo.phone}}</div>
								</div>
								<div class="column">
									<div class="label">推荐人：</div><div class="text">{{showUserInfo.rec}}</div>
								</div>
							</div>
							<div class="section">
								<div class="column">
									<div class="label">出生日期：</div><div class="text">{{showUserInfo.birthday}}</div>
								</div>
								<div class="column">
									<div class="label">客户来源：</div><div class="text">{{showUserInfo.source}}</div>
								</div>
							</div>
						</div>
					</div>
				</div><!--div end-->
				<!-- 就诊事项 -->
				<div class="check_info">
					<div class="top">
						<span class="red">*</span>
						<span class="title">预约类型</span>
					</div><!-- top end -->
					<div class="items">
						<div class="item" :class="{selected:c.id==item_id}" v-for="c,index of items" @click="checkItem(c)">{{c.name}}</div>
					</div>
				</div><!--check_info end-->
				
				
		    </Col><!--col end-->
			<Col v-bind="grid">
			    <div class="right_info">
			    	<div class="keshi">
						<div class="item" :class="{selected:c.ks_id==ks_id}" v-for="c,index of schedules" @click="checkKsItem(c)">{{c.ks_name}}</div>
					</div>
			    	<div class="doctor">
						<div class="item" :class="{selected:c.doc_id==doc_id}" v-for="c,index of doctors" @click="checkDocItem(c)">{{c.doc_name}}</div>
					</div>
			    	<div class="date">
						<div class="header">
							<div class="item" v-if="c.type==0" :class="{selected:c.id==ampm_id}" v-for="c,index of ampms" @click="checkAmpmItem(c)">上午</div>
							<div class="item" v-if="c.type==1" :class="{selected:c.id==ampm_id}" v-for="c,index of ampms" @click="checkAmpmItem(c)">下午</div>
						</div>
						<div class="time">
							<div class="subitem" :class="{selected:c.id==time_id}" v-for="c,index of times" @click="checkTimeItem(c)">{{c.no}}号 {{c.start_time}}-{{c.end_time}}</div>
						</div>
					</div>
			    </div>
			</Col><!--col end-->
		</Row><!--row end-->
      </Form><!--form end-->
	  <div slot="footer" class="footer">
		  <div class="reg_info"><span class="title">挂号费用：</span><span class="text">{{amount}}元</span></div>
		  <div>
			<Button @click="cancel()">取消</Button>
			<Button type="primary" @click="handleSubmit()">保存</Button>
		  </div>
	  </div>
    </Modal>
    <Modal v-model="modal12" scrollable width="600" title="图标选择" footer-hide>
      <Input
        v-model="iconVal"
        placeholder="输入关键词搜索,注意全是英文"
        clearable
        style="width: 300px"
        @on-change="upIcon(iconVal)"
        ref="search"
      />
      <div class="trees-coadd">
        <div class="scollhide">
          <div class="iconlist">
            <ul class="list-inline">
              <li class="icons-item" v-for="(item, i) in list" :key="i" :title="item.type">
                <Icon :type="item.type" @click="iconChange(item.type)" class="ivu-icon" />
              </li>
            </ul>
          </div>
        </div>
      </div>
    </Modal>
  </div>
</template>

<script>
import { ListScheduleApi,saveApi} from '@/api/register';
import { ListCateApi} from '@/api/subcateitem';
import { ListSearchApi,GetApi} from '@/api/archives';

import icon from '@/utils/icon';
import { getCookies } from '@/libs/util';
export default {
  name: 'menusFrom',
  props: {
    formValidate: {
      type: Object,
      default: null,
    },
    titleFrom: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      arrs: [],
      searchRule: '',
      iconVal: '',
      grid: {
        xl: 12,
        lg: 12,
        md: 12,
        sm: 24,
        xs: 24,
      },
      modals: false,
      modal12: false,
      ruleValidate: {
		uid: [{ required: true, message: '请选择用户', trigger: 'blur' }],
		item_id: [{ required: true, message: '请选择就诊事项', trigger: 'blur' }],
		time_id: [{ required: true, message: '请选择预约编号', trigger: 'blur' }],
      },
      // types: [],
      // status: [],
      // citys: [],
      // companys: [],
      users: [],
	  items:[],
	  item_id:0,//就诊事项
	  //排班科室
	  schedules:[],
	  ks_id:0,
	  //科室下的医生
	  doctors:[],
	  doc_id:0,
	  //医生排的上下午
	  ampms:[],
	  ampm_id:0,
	  //上下午的号码
	  times:[],
	  time_id:0,
	  amount:0.00,//挂号费
      valids: false,
      list: icon,
      authType: true,
      search: icon,
	  header: {},
      fileUrl: '/adminapi/file/upload_file/1',
	  files:[],
	  sche_date:'',
	  showUserInfo:{
		  name:'-',
		  sex:'-',
		  age:'-',
		  no:'-',
		  date:'-',
		  phone:'-',
		  rec:'-',
		  birthday:'-',
		  source:'-'
	  }
    };
  },
  watch: {
    'formValidate.header': function (n) {
      this.formValidate.is_header = n ? 1 : 0;
    },
    'formValidate.auth_type': function (n) {
      if (n === undefined) {
        n = 1;
      }
      this.authType = n === 1;
    },
    'formValidate.data': function (n) {},
	
	'formValidate.uid': function (n) {
		console.info(n)
		if(n){
			this.getUserInfo(n);
		}
	},
	
  },
  computed: {
	  
  },
  methods: {
    init() {
      this.searchRule = '';
      this.arrs = [];
    },
	selectDate(e){
		this.sche_date = e
		//重新加载数据
		this.getSchedule();
	},
	handleChange(){
		console.log('Selected value changed to:');
	},
	searchUsers(){
		let that = this;
		ListSearchApi({})
		.then(async (res) => {
			that.users = res.data;
			
		})
		.catch((res) => {
		  this.$Message.error(res.msg);
		});
	},
	//就诊项目
	checkItem(row){
		this.item_id = row.id;
	},
	//科室
	checkKsItem(row){
		if(this.ks_id == row.ks_id)return;
		this.ks_id = row.ks_id;
		this.doctors = row.doctors;
		let that = this;
		//医生
		if(row.doctors && row.doctors.length > 0){
			let doctor = row.doctors[0];
			that.doc_id = doctor.doc_id;
			//上下午
			if(doctor.items && doctor.items.length > 0){
				let item = doctor.items[0];
				that.ampm_id = item.id;
				that.ampms = doctor.items;
				
				//排号
				if(item.nos && item.nos.length > 0){
					let no = item.nos[0];
					that.time_id = no.id;
					that.times = item.nos;
					that.amount = no.amount;
				}
			}
		}
	},
	//医生
	checkDocItem(row){
		if(this.doc_id == row.doc_id)return;
		this.doc_id = row.doc_id;
		this.ampms = row.items;
		let that = this;
		//上下午
		if(row.items && row.items.length > 0){
			let item = row.items[0];
			that.ampm_id = item.id;
			
			//排号
			if(item.nos && item.nos.length > 0){
				let no = item.nos[0];
				that.time_id = no.id;
				that.times = item.nos;
				that.amount = no.amount;
			}
		}
	},
	//上下午
	checkAmpmItem(row){
		if(this.ampm_id == row.id)return;
		this.ampm_id = row.id;
		this.times = row.nos;
		let that = this;
		//排号
		if(row.nos && row.nos.length > 0){
			let no = row.nos[0];
			that.time_id = no.id;
			that.amount = no.amount;
		}
	},
	//时间
	checkTimeItem(row){
		this.time_id = row.id;
		this.amount = row.amount;
	},
	// 上传成功
	handleSuccess(res, file) {
	  if (res.status === 200) {
		this.files.push(res.data.data);
	    this.$Message.success(res.msg);
	  } else {
	    this.$Message.error(res.msg);
	  }
	},
	handleFormatError(file) {
		
	},
	handleMaxSize(file) {
	  this.$Message.warning('请上传文件30M以内的文件');
	},
	delFile(index){
		this.files.splice(index,1);
	},
	// 上传头部token
	getToken() {
	  this.header['Authori-zation'] = 'Bearer ' + getCookies('token');
	  console.info(this.header);
	},
    handleCreate1(val) {
      this.headerOptionsList.push({
        value: val,
        label: val,
      });
    },
    // 获取客户数据
    getAddFrom() {
		let that = this;
		let nowdate = new Date();
		nowdate.setDate(nowdate.getDate());
		 let y = nowdate.getFullYear();
		 let m = nowdate.getMonth() + 1 < 10 ? "0" + (nowdate.getMonth() + 1) : nowdate.getMonth() + 1;
		 let d = nowdate.getDate() < 10 ? "0" + nowdate.getDate() : nowdate.getDate();
		 let formatwdate = y + '-' + m + '-' + d;
		this.sche_date = formatwdate;
		
		this.getSchedule();
		
		ListCateApi({})
		.then(async (res) => {
			that.items = res.data;
			
		})
		.catch((res) => {
		  this.$Message.error(res.msg);
		});
		
		this.searchUsers();
		
    },
	resetData(){
		//科室
		this.ks_id = 0;
		//医生
		this.doc_id = [];
		this.doctors = [];
		//上下午
		this.ampm_id = 0;
		this.ampms = [];
		//排号
		this.time_id = 0;
		this.times = [];
		
		this.amount = 0;
	},
	//获取排班
	getSchedule(){
		if(!this.sche_date){
			this.sche_date = '';
		}
		let that = this;
		ListScheduleApi({
			sche_time:this.sche_date
		})
		.then(async (res) => {
			that.resetData();
			that.schedules = res.data;
			//取默认数据
			if(that.schedules && that.schedules.length > 0){
				//科室
				let ks = that.schedules[0];
				that.ks_id = ks.ks_id;
				//医生
				if(ks.doctors && ks.doctors.length > 0){
					let doctor = ks.doctors[0];
					that.doc_id = doctor.doc_id;
					that.doctors = ks.doctors;
					//上下午
					if(doctor.items && doctor.items.length > 0){
						let item = doctor.items[0];
						that.ampm_id = item.id;
						that.ampms = doctor.items;
						
						//排号
						if(item.nos && item.nos.length > 0){
							let no = item.nos[0];
							that.time_id = no.id;
							that.times = item.nos;
							that.amount = no.amount;
						}
					}
				}
			}
		})
		.catch((res) => {
		  this.$Message.error(res.msg);
		});
	},
	cancel(){
		this.modals = false;
	},
    // 提交
    handleSubmit() {
	 this.formValidate.item_id = this.item_id;
	 this.formValidate.time_id = this.time_id;
	 this.formValidate.sche_date = this.sche_date;
	 if (!this.formValidate.uid) return this.$Message.error('请选择用户');
	 if (!this.formValidate.item_id) return this.$Message.error('请选择就诊事项');
	 if (!this.formValidate.time_id) return this.$Message.error('请选择预约号');
     saveApi(this.formValidate).then(async (res) => {
         this.$Message.success(res.msg);
         this.modals = false;
         this.$emit('getList');
         this.getAddFrom();
       })
       .catch((res) => {
         this.$Message.error(res.msg);
      });
    },
    handleReset() {
      this.modals = false;
      this.authType = true;
      this.$refs['formValidate'].resetFields();
      this.$emit('clearFrom');
    },
	addArchive(){
		console.info(333)
		this.$emit('showArchiveModal');
	},
	getUserInfo(id){
		
		GetApi(id).then(async (res) => {
			console.info(res)
			if(res.data){
				this.showUserInfo.name = res.data.name;
				this.showUserInfo.age = res.data.age + '岁';
				this.showUserInfo.sex = res.data.sex == 0?"男":"女";
				this.showUserInfo.no = res.data.no;
				this.showUserInfo.created_time = res.data.arc_date;
				this.showUserInfo.phone = res.data.phone;
				this.showUserInfo.birthday = res.data.birthday;
			}
		}).catch((res) => {
		    this.$Message.error(res.msg);
		});
	}
  },
  created() {
    this.list = this.search;
    this.getToken();
    this.getAddFrom();
  },
};
</script>

<style scoped>
.trees-coadd {
  width: 100%;
  height: 500px;
  border-radius: 4px;
  overflow: hidden;
}
.scollhide {
  width: 100%;
  height: 100%;
  overflow: auto;
  margin-left: 18px;
  padding: 10px 0 10px 0;
  box-sizing: border-box;
}
.content {
  font-size: 12px;
}

.time {
  font-size: 12px;
  color: #2d8cf0;
}

.icons-item {
  float: left;
  margin: 6px 6px 6px 0;
  width: 53px;
  text-align: center;
  list-style: none;
  cursor: pointer;
  height: 50px;
  color: #5c6b77;
  transition: all 0.2s ease;
  position: relative;
  padding-top: 10px;
}
.icons-item .ivu-icon {
  font-size: 16px;
}
.search-rule {
  display: flex;
  align-items: center;
  padding: 10px;
  background-color: #f2f2f2;
}
.rule {
  display: flex;
  flex-wrap: wrap;
  max-height: 700px;
  overflow: scroll;
}
/*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/
.rule::-webkit-scrollbar {
  width: 10px;
  height: 10px;
  background-color: #f5f5f5;
}

/*定义滚动条轨道 内阴影+圆角*/
.rule::-webkit-scrollbar-track {
  border-radius: 4px;
  background-color: #f5f5f5;
}

/*定义滑块 内阴影+圆角*/
.rule::-webkit-scrollbar-thumb {
  border-radius: 4px;
  background-color: #555;
}
.rule-list {
  background-color: #f2f2f2;
  width: 32%;
  margin: 5px;
  border-radius: 3px;
  padding: 10px;
  color: #333;
  cursor: pointer;
  transition: all 0.1s;
}
.rule-list:hover {
  background-color: #c5d1dd;
}
.rule-list div {
  white-space: nowrap;
}
.select-rule {
  background-color: #c5d1dd;
}
.add {
  display: flex;
  align-items: center;
}
.df {
  display: flex;
  justify-content: center;
}

.file{
	display: flex;
	flex-direction: row;
	justify-content: space-between;
	align-items: center;
}

.file:hover{
	background-color: #edf7fe;
}
.close-bg{
	display: inline-block;
	width: 35px;
	height: 35px;
	display: flex;
	flex-direction: row;
	justify-content: center;
	align-items: center;
	margin-right: 15px;;
}

.close-bg:hover{
	cursor: pointer;
}

.close {
	display: inline-block;
	width: 15px;
	height: 2px;
	background: red;
	transform: rotate(45deg);
}

.close::after {
	content: '';
	display: block;
	width: 15px;
	height: 2px;
	background: red;
	transform: rotate(-90deg);
}

.input_top{
	display: flex;
	flex-direction: row;
	padding: 0px 12px 20px 12px;
	align-items: center;
	flex: 1;
}

.input_top .left{
	display: flex;
	flex-direction: row;
	flex:1;
}
.input_top .left .button{
	width: 100px;
	margin-left: 15px;
	background-color: white;
	border: 1px solid #2d8cf0;
	color:#2d8cf0;
	font-weight: bold;
	font-size: 14px;
}

.input_top .right{
	display: flex;
	flex-direction: row;
	margin-left: 30px;
	align-items: center;
	flex:1;
}
.input_top .right .label{
	font-size: 14px;
	width: 80px;
}

.input_top .right .date{
	width:100%;
}

.user_info{
	display: flex;
	border: 1px solid #dfdfdf;
	padding: 10px;
}
.user_info .avatar{
	width:50px;
	height:50px;
	border-radius: 25px;
	background-color: white;
}
.user_info .info_defail{
	display: flex;
	flex-direction: column;
	margin-left: 15px;
}

.user_info .info_defail .name{
	font-size: 20px;font-weight: bold
}
.user_info .info_defail .property_info{
	display: flex;
	flex-direction: column;
}
.user_info .info_defail .property_info .section{
	display: flex;
	flex-direction: row; 
	margin-top: 10px;
}
.user_info .info_defail .property_info .section .column{
	display: flex;
	flex-direction: row;
	width: 190px;
}
.user_info .info_defail .property_info .section .column .label{
	width: 60px;
	text-align: right;
}
/** 就诊事项 **/
.check_info{
	display: flex;
	border: 1px solid #dfdfdf;
	padding: 10px;
	margin-top: 10px;
	flex-direction: column;
}
.check_info .top{
	display: flex;
	flex-direction: row;
}
.check_info .top .red{
	color: red;
	font-size: 20px;
}
.check_info .top .title{
	font-size: 16px;
	font-weight: bold;
}
.check_info .items{
	display: flex;
	flex-direction: row;
	flex-wrap: wrap;
	justify-content: space-between;
}
.check_info .items .item{
	width:49%;
	height:35px;
	text-align: center;
	line-height: 35px;
	border: 1px solid #f1f1f1;
	margin-bottom: 5px;
	cursor: pointer;
}
.check_info .items .item:hover{
	font-weight: bold;
}
.check_info .items .selected{
	color: white;
	background-color: #2d8cf0;
}
.footer{
	display: flex;
	flex-direction: row;
	justify-content: space-between;
}
.footer .reg_info{
	display: flex;
	flex-direction: row;
	align-items: center;
}
.footer .reg_info .title{
	font-size: 14px;
}
.footer .reg_info .text{
	font-size: 14px;
	font-weight: bold;
	color: #2d8cf0;
}
.right_info{
	display: flex;
	flex-direction: row;
	justify-content: space-around;
}
.right_info .keshi{
	border: 1px solid #dfdfdf;
	width: 33%;
	height: 318px;
	overflow-y: auto;
}
.right_info .keshi .item{
	font-size: 14px;
	padding: 5px 10px;
	border-bottom: 1px solid #dfdfdf;
	cursor: pointer;
}
.right_info .keshi .selected{
	color: white;
	background-color: #2d8cf0;
}
.right_info .doctor{
	border: 1px solid #dfdfdf;
	width: 33%;
	height: 318px;
	overflow-y: auto;
}
.right_info .doctor .item{
	font-size: 14px;
	padding: 5px 10px;
	border-bottom: 1px solid #dfdfdf;
	cursor: pointer;
}
.right_info .doctor .selected{
	color: white;
	background-color: #2d8cf0;
}

.right_info .date{
	border: 1px solid #dfdfdf;
	width: 33%;
	height: 318px;
	overflow-y: auto;
}
.right_info .date .header{
	display:flex;
	flex-direction: row;
	border-bottom: 1px solid #dfdfdf;
}
.right_info .date .header .item{
	font-size: 14px;
	padding: 5px 10px;
	cursor: pointer;
}

.right_info .date .header .selected{
	border-bottom: 2px solid #2d8cf0;
	font-weight: bold;
}

.right_info .date .time{
	display: flex;
	flex-direction: column;
}
.right_info .date .time .subitem{
	font-size: 14px;
	padding: 3px 10px;
	border-bottom: 1px solid #dfdfdf;
	cursor: pointer;
	color: black;
}

.right_info .date .time .selected{
	color: white;
	background-color: #2d8cf0;
}
</style>
